import * as step from '@flow-step/step-toolkit'
import path from 'path';

export async function installHelm(helmVersion: string): Promise<void> {
    step.info(`Going to install helm with version ${helmVersion}`)

    try {
        let platform: string = step.platform.getPlatform()
        let toolSuffix = getToolSuffix(platform)

        const latestPatchVersion = await step.tool.formatToLatestPatchVersion('helm', 'v', helmVersion)
        step.info(`The latest Version is ${latestPatchVersion}`)
        const helmInstalledPath = await step.tool.install('helm', latestPatchVersion, toolSuffix)
        step.info('installed Helm to ' + helmInstalledPath)
        step.addPath(helmInstalledPath)
    } catch (err) {
        throw new Error(`Install Helm ${helmVersion} failed, ${err}`)
    }

    step.info(`Install Helm ${helmVersion} successfully!`)
}

export function setupHelm(helmCachedPath: string): void {
    let goRootPath = helmCachedPath
    let goBinPath = path.join(goRootPath, 'bin')
    step.info(`go bin path: ${goBinPath}`)
    step.addPath(goBinPath)
}

export function getToolSuffix(platform: string): string {
    return platform === step.platform.PLATFORM_WIN ? "zip" : "tar.gz";
}
